package com.lmabbe.common.global.context.metadata;

import cn.hutool.extra.spring.SpringUtil;
import com.lmabbe.common.global.constant.RedisKeyConstant;
import com.lmabbe.common.global.context.LmabbeMetadata;
import com.lmabbe.common.global.context.LmabbeSessionUser;
import com.lmabbe.common.global.context.metadata.client.OauthClient;
import com.lmabbe.common.global.context.metadata.dict.SystemDict;
import com.lmabbe.common.global.context.metadata.param.SystemParam;
import com.lmabbe.common.global.context.metadata.tenant.TenantInfo;
import com.lmabbe.common.global.context.session.DefaultLmabbeSessionUserImpl;
import com.lmabbe.common.global.dict.BaseDict;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 获取元数据的实现
 *
 * @author lmabbe
 */
@Component
@AllArgsConstructor
public class LmabbeMetadataRuntime implements LmabbeMetadata {

    private final ValueOperations stringOperation;

    @Override
    public TenantInfo currentTenant() {
        return (TenantInfo) stringOperation.get(String.format(RedisKeyConstant.System.Tenant.TENANT_INFO, getLmabbeSessionUser().getTenantId()));
    }

    @Override
    public List<OauthClient> currentTenantClient() {
        return (List<OauthClient>) stringOperation.get(String.format(RedisKeyConstant.System.Tenant.TENANT_CLIENT, getLmabbeSessionUser().getTenantId()));
    }

    @Override
    public List<SystemParam> currentSysParam() {
        return (List<SystemParam>) stringOperation.get(String.format(RedisKeyConstant.System.Tenant.TENANT_PARAM, getLmabbeSessionUser().getTenantId()));
    }

    @Override
    public List<SystemDict> currentTenantDict() {
        return (List<SystemDict>) stringOperation.get(String.format(RedisKeyConstant.System.Tenant.TENANT_DICT, getLmabbeSessionUser().getTenantId()));
    }

    @Override
    public String getParamByKey(String key) {
        Optional<SystemParam> first = currentSysParam().stream().filter(s -> s.getParamKey().equals(key)).findFirst();
        return first.map(SystemParam::getParamValue).orElse(null);
    }

    @Override
    public BaseDict getDictValByKey(String key) {
        List<SystemDict.Item> list = new ArrayList<>();
        currentTenantDict().stream().forEach(s -> list.addAll(s.getItems()));
        Optional<SystemDict.Item> first = list.stream().filter(s -> s.getItemCode().equals(key)).findFirst();
        if (first.isPresent()) {
            SystemDict.Item item = first.get();
            return new BaseDict(item.getItemCode(), item.getItemName());
        } else {
            return null;
        }
    }

    public LmabbeSessionUser getLmabbeSessionUser() {
        LmabbeSessionUser bean;
        try {
            bean = SpringUtil.getBean(LmabbeSessionUser.class);
            if (bean != null) {
                bean = new DefaultLmabbeSessionUserImpl();
            }
        } catch (Exception e) {
            bean = new DefaultLmabbeSessionUserImpl();
        }
        return bean;
    }
}
